﻿namespace TPXSDK.NET.Share;

/// <summary>
/// 文件系统工具类
/// </summary>
public class FileUtils
{
    /// <summary>
    /// 清空指定的文件夹，但不删除文件夹
    /// </summary>
    public static void ClearFolder(string dir)
    {
        foreach (var d in Directory.GetFileSystemEntries(dir))
        {
            if (File.Exists(d))
            {
                var fi = new FileInfo(d);
                if (fi.Attributes.ToString().ToLower().Contains("readonly"))
                    fi.Attributes = FileAttributes.Normal;
                File.Delete(d);//直接删除其中的文件  
            }
            else
            {
                var d1 = new DirectoryInfo(d);
                if (d1.GetFiles().Length != 0)
                {// 递归删除子文件夹
                    ClearFolder(d1.FullName);
                }
                Directory.Delete(d);
            }
        }
    }

    /// <summary>
    /// 删除文件夹及其内容
    /// </summary>
    public static void DeleteFolder(string dir)
    {
        if (!Directory.Exists(dir))
        {
            return;
        }

        foreach (var d in Directory.GetFileSystemEntries(dir))
        {
            if (File.Exists(d))
            {
                var fi = new FileInfo(d);
                if (fi.Attributes.ToString().Contains("readonly", StringComparison.CurrentCultureIgnoreCase))
                {
                    fi.Attributes = FileAttributes.Normal;
                }
                File.Delete(d);//直接删除其中的文件  
            }
            else
            {
                DeleteFolder(d);//递归删除子文件夹
            }
        }

        Directory.Delete(dir);
    }

    /// <summary>
    /// 检查文件名是否有效
    /// </summary>
    public static bool IsValid(string fileName)
    {
        var isValid = true;
        var c1 = Path.GetInvalidPathChars();
        var c2 = Path.GetInvalidFileNameChars();
        if (string.IsNullOrEmpty(fileName))
        {
            isValid = false;
        }
        else
        {
            if (c1.Any(fileName.Contains))
            {
                isValid = false;
            }

            if (c2.Any(fileName.Contains))
            {
                isValid = false;
            }
        }

        return isValid;
    }

    /// <summary>
    /// 复制文件夹（包含子文件夹）
    /// </summary>
    public static bool CopyDirectory(string srcPath, string desPath, bool overwrite, string[]? skipFile = null)
    {
        var ret = true;

        try
        {
            srcPath = srcPath.EndsWith(@"\") ? srcPath : srcPath + @"\";
            desPath = desPath.EndsWith(@"\") ? desPath : desPath + @"\";

            if (Directory.Exists(srcPath))
            {
                if (Directory.Exists(desPath) == false)
                    Directory.CreateDirectory(desPath);

                foreach (var fls in Directory.GetFiles(srcPath))
                {
                    var fiInfo = new FileInfo(fls);
                    if (skipFile != null && skipFile.Any(s => s == fiInfo.Name)) continue;
                    fiInfo.CopyTo(desPath + fiInfo.Name, overwrite);
                }
                foreach (var drs in Directory.GetDirectories(srcPath))
                {
                    var drInfo = new DirectoryInfo(drs);
                    if (CopyDirectory(drs, desPath + drInfo.Name, overwrite) == false)
                        ret = false;
                }
            }
            else
            {
                ret = false;
            }
        }
        catch (Exception)
        {
            ret = false;
        }

        return ret;
    }
}
